package com.ithuangyl.common.web.chromeclient;

import android.net.Uri;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebView;

import com.ithuangyl.common.web.base.BaseWebView;
import com.ithuangyl.common.web.callback.IFileChooserInterface;

/**
 * author : itHuang
 * e-mail : ithuangyl@163.com
 * date   : 2020/6/22 11:30
 */

public class WebChromeClientImpl extends WebChromeClient {

    private BaseWebView webView;
    private IFileChooserInterface iFileChooserInterface;

    public WebChromeClientImpl(BaseWebView webView, IFileChooserInterface iFileChooserInterface) {
        this.webView = webView;
        this.iFileChooserInterface = iFileChooserInterface;
    }

    // For Android 3.0-
    public void openFileChooser(ValueCallback<Uri> uploadMsg) {
        webView.mUploadMessage = uploadMsg;
        iFileChooserInterface.onShowFileChooser(uploadMsg, null, null, null, null, null);
    }

    // For Android 3.0+
    public void openFileChooser(ValueCallback uploadMsg, String acceptType) {
        webView.mUploadMessage = uploadMsg;
        iFileChooserInterface.onShowFileChooser(uploadMsg, acceptType, null, null, null, null);
    }

    //For Android 4.1.1+
    public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
        webView.mUploadMessage = uploadMsg;
        iFileChooserInterface.onShowFileChooser(uploadMsg, acceptType, capture, null, null, null);
    }

    // For Android 5.0+
    public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) {
        ((BaseWebView) webView).mUploadCallbackAboveL = filePathCallback;
        iFileChooserInterface.onShowFileChooser(null, null, null, (BaseWebView) webView, filePathCallback, fileChooserParams);
        return true;
    }
}
